<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class UserController extends Controller{
    public function userRows(){
        $rows=DB::table('user')->get();
        $role=DB::table('role')->get();
        $role_name=[];
        foreach ($rows as $k=>$v){
            foreach ($role as $k1=>$v1){
                if($v->role_id == $v1->id){
                    $role_name=$v1->role_name;
                }
            }
            $rows[$k]->role_name=$role_name;
        }
        $data=[
            "code"=>0,
            "msg"=>'',
            "count"=>count($rows),
            "data"=>$rows
//            "data"=>$this->userPage($page,$limit),
//            "limit"=>$limit
        ];
        return json_encode($data);
    }
    //冻结用户
    public function frz(Request $request){
        $FRZ['FRZ']=$request->post('FRZ');
        $id=$request->post('id');
        $rows=DB::table('user')->where('id',$id)->update($FRZ);

    }
    //选择角色
    public function user_select(){
        $rows=DB::table('role')->get();
        return $rows;
    }
    //添加用户
    public function user_insert(Request $request){
        $data=$request->post('result');
        $data['ip']=$request->ip();
        $rows=DB::table('user')->insert($data);
        if($rows){
            return 1;
        }else{
            return 0;
        }
    }
    //编辑用户
    public function user_edit($id){
        $rows=DB::table('user')->where('id',$id)->get()->map(function ($value) {
            return (array)$value;
        })->toArray();
        return view('admin.user_edit',['rows'=>$rows[0]]);
    }
    //用户角色回显
    public function user_role_select($id){
        $rows=DB::table('role')->get();
        $rows2=DB::table('user')->where('id',$id)->first();
        foreach ($rows as $k=>$v){
            if($v->id==$rows2->role_id){
                $rows[$k]->selected=true;
            }
        }
        return $rows;
    }
    //修改用户
    public function user_update(Request $request){
        $data=$request->post('result');
        $rows=DB::table('user')->where('id',$data['id'])->update($data);
        if($rows){
            return 1;
        }
    }
}